<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>精美侧边栏 with Tooltip</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f7fa;
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏容器 */
        .sidebar {
            width: 250px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            height: 100vh;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            box-shadow: 2px 0 10px rgba(0,0,0,0.1);
            position: relative;
            z-index: 100;
        }

        /* 折叠状态 */
        .sidebar.collapsed {
            width: 70px;
        }

        /* 侧边栏头部 */
        .sidebar-header {
            padding: 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }

        .sidebar.collapsed .sidebar-header {
            justify-content: center;
        }

        .logo {
            font-size: 24px;
            font-weight: bold;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .logo-icon {
            font-size: 28px;
        }

        .sidebar.collapsed .logo-text {
            display: none;
        }

        /* 折叠按钮 */
        .toggle-btn {
            background: rgba(255,255,255,0.2);
            border: none;
            color: white;
            width: 30px;
            height: 30px;
            border-radius: 50%;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
        }

        .toggle-btn:hover {
            background: rgba(255,255,255,0.3);
            transform: rotate(180deg);
        }

        .sidebar.collapsed .toggle-btn {
            transform: rotate(180deg);
        }

        .sidebar.collapsed .toggle-btn:hover {
            transform: rotate(0deg);
        }

        /* 菜单列表 */
        .menu-list {
            list-style: none;
            padding: 20px 0;
        }

        .menu-item {
            position: relative;
            padding: 15px 20px;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 15px;
            transition: all 0.3s ease;
        }

        .menu-item:hover {
            background: rgba(255,255,255,0.1);
        }

        .menu-item.active {
            background: rgba(255,255,255,0.2);
            border-left: 4px solid white;
        }

        .menu-icon {
            font-size: 20px;
            min-width: 20px;
            text-align: center;
        }

        .menu-text {
            font-size: 16px;
            white-space: nowrap;
            overflow: hidden;
            transition: opacity 0.3s ease;
        }

        .sidebar.collapsed .menu-text {
            opacity: 0;
            width: 0;
            display: none;
        }

        /* Tooltip 样式 */
        .tooltip {
            position: fixed;
            background: white;
            color: #333;
            padding: 12px 16px;
            border-radius: 8px;
            box-shadow: 0 10px 25px rgba(0,0,0,0.2);
            font-size: 14px;
            white-space: nowrap;
            z-index: 1000;
            opacity: 0;
            visibility: hidden;
            transform: translateX(-10px);
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            pointer-events: none;
        }

        .tooltip.show {
            opacity: 1;
            visibility: visible;
            transform: translateX(0);
        }

        /* Tooltip 左侧箭头 */
        .tooltip::before {
            content: '';
            position: absolute;
            left: -8px;
            top: 50%;
            transform: translateY(-50%);
            width: 0;
            height: 0;
            border-top: 8px solid transparent;
            border-bottom: 8px solid transparent;
            border-right: 8px solid white;
        }

        /* 主内容区域 */
        .main-content {
            flex: 1;
            padding: 30px;
            background: #f5f7fa;
        }

        .content-header {
            margin-bottom: 30px;
        }

        .content-header h1 {
            color: #333;
            margin-bottom: 10px;
        }

        .content-header p {
            color: #666;
            line-height: 1.6;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .sidebar {
                width: 70px;
            }
            
            .sidebar:not(.collapsed) {
                width: 250px;
            }
            
            .menu-text {
                opacity: 0;
                width: 0;
                display: none;
            }
            
            .sidebar:not(.collapsed) .menu-text {
                opacity: 1;
                width: auto;
                display: block;
            }
        }
    </style>
</head>
<body>
    <!-- 侧边栏 -->
    <div class="sidebar" id="sidebar">
        <div class="sidebar-header">
            <div class="logo">
                <span class="logo-icon">🚀</span>
                <span class="logo-text">Dashboard</span>
            </div>
            <button class="toggle-btn" id="toggleBtn">◀</button>
        </div>
        
        <ul class="menu-list">
            <li class="menu-item active" data-tooltip="首页">
                <span class="menu-icon">🏠</span>
                <span class="menu-text">首页</span>
            </li>
            <li class="menu-item" data-tooltip="数据分析">
                <span class="menu-icon">📊</span>
                <span class="menu-text">数据分析</span>
            </li>
            <li class="menu-item" data-tooltip="用户管理">
                <span class="menu-icon">👥</span>
                <span class="menu-text">用户管理</span>
            </li>
            <li class="menu-item" data-tooltip="设置">
                <span class="menu-icon">⚙️</span>
                <span class="menu-text">设置</span>
            </li>
            <li class="menu-item" data-tooltip="帮助">
                <span class="menu-icon">❓</span>
                <span class="menu-text">帮助</span>
            </li>
            <li class="menu-item" data-tooltip="退出">
                <span class="menu-icon">🚪</span>
                <span class="menu-text">退出</span>
            </li>
        </ul>
    </div>

    <!-- 主内容区域 -->
    <div class="main-content">
        <div class="content-header">
            <h1>精美侧边栏示例</h1>
            <p>这是一个具有水平折叠功能的精美侧边栏。当侧边栏折叠时，鼠标悬浮在菜单项上会显示带有箭头的Tooltip提示。</p>
        </div>
        
        <div style="background: white; padding: 30px; border-radius: 10px; box-shadow: 0 5px 15px rgba(0,0,0,0.08);">
            <h2>功能说明</h2>
            <ul style="margin-top: 20px; padding-left: 20px; line-height: 1.8;">
                <li>点击左上角按钮可以折叠/展开侧边栏</li>
                <li>侧边栏折叠后，鼠标悬浮在菜单项上会显示Tooltip</li>
                <li>Tooltip具有美观的样式和指向箭头</li>
                <li>所有过渡动画都流畅自然</li>
                <li>响应式设计，适配不同屏幕尺寸</li>
            </ul>
        </div>
    </div>

    <!-- Tooltip 元素 -->
    <div class="tooltip" id="tooltip"></div>

    <script>
        // 获取DOM元素
        const sidebar = document.getElementById('sidebar');
        const toggleBtn = document.getElementById('toggleBtn');
        const tooltip = document.getElementById('tooltip');
        const menuItems = document.querySelectorAll('.menu-item');

        // 切换侧边栏折叠状态
        function toggleSidebar() {
            sidebar.classList.toggle('collapsed');
            
            // 更新按钮图标方向
            if (sidebar.classList.contains('collapsed')) {
                toggleBtn.textContent = '▶';
            } else {
                toggleBtn.textContent = '◀';
            }
        }

        // 显示Tooltip
        function showTooltip(element, text) {
            // 设置Tooltip文本
            tooltip.textContent = text;
            
            // 获取菜单项的位置信息
            const rect = element.getBoundingClientRect();
            
            // 计算Tooltip位置（在菜单项右侧）
            const tooltipX = rect.right + 10; // 右侧偏移10px
            const tooltipY = rect.top + (rect.height / 2) - (tooltip.offsetHeight / 2); // 垂直居中
            
            // 设置Tooltip位置
            tooltip.style.left = tooltipX + 'px';
            tooltip.style.top = tooltipY + 'px';
            
            // 显示Tooltip
            tooltip.classList.add('show');
        }

        // 隐藏Tooltip
        function hideTooltip() {
            tooltip.classList.remove('show');
        }

        // 为切换按钮添加点击事件
        toggleBtn.addEventListener('click', toggleSidebar);

        // 为每个菜单项添加鼠标事件
        menuItems.forEach(item => {
            // 鼠标进入事件
            item.addEventListener('mouseenter', function(e) {
                // 只有在侧边栏折叠时才显示Tooltip
                if (sidebar.classList.contains('collapsed')) {
                    const tooltipText = this.getAttribute('data-tooltip') || this.querySelector('.menu-text').textContent;
                    showTooltip(this, tooltipText);
                }
            });
            
            // 鼠标离开事件
            item.addEventListener('mouseleave', function(e) {
                hideTooltip();
            });
            
            // 点击事件（用于激活菜单项）
            item.addEventListener('click', function(e) {
                // 移除所有菜单项的active类
                menuItems.forEach(menuItem => {
                    menuItem.classList.remove('active');
                });
                
                // 为当前点击的菜单项添加active类
                this.classList.add('active');
                
                // 隐藏Tooltip
                hideTooltip();
            });
        });

        // 当鼠标在Tooltip上移动时也保持显示
        tooltip.addEventListener('mouseenter', function(e) {
            if (sidebar.classList.contains('collapsed')) {
                this.classList.add('show');
            }
        });

        tooltip.addEventListener('mouseleave', function(e) {
            this.classList.remove('show');
        });

        // 窗口大小改变时隐藏Tooltip
        window.addEventListener('resize', function() {
            hideTooltip();
        });

        // 点击页面其他地方时隐藏Tooltip
        document.addEventListener('click', function(e) {
            if (!e.target.closest('.menu-item') && !e.target.closest('.tooltip')) {
                hideTooltip();
            }
        });
    </script>
</body>
</html>
